﻿@{
    ViewBag.Title = "Management";
    Layout = "~/Views/Shared/ListLayout.cshtml";
}
<table id="tg" class="easyui-treegrid"
       data-options="url:'@Url.Action("GetAllOrganizationsForTreeGrid")',rownumbers: true,idField: 'Id',parentField: 'ParentId',treeField: 'Name',toolbar:'#tb',fitColumns:true,fit:true">
    <thead>
        <tr>
            <th data-options="field:'Name',width:80,editor:{type:'validatebox',options:{required:true}}">组织名称</th>
            <th data-options="field:'DisplayOrder',width:100,editor:{type:'numberbox',options:{required:true}}">顺序</th>
            <th data-options="field:'Remark',width:100,editor:'text'">备注</th>
        </tr>
    </thead>
</table>
<!--toolbar-->
<div id="tb" style="padding:5px;height:auto">
    <div class="datagrid-toolbar-toolField">
        <div style="width: 70%; float: left">
            @Html.Action("RenderToolbar", "Common", new { area = "" })
            @*<a href="#" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="onclickAdd()">添加</a>
                <a href="#" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="edit()">编辑</a>
                <a href="#" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="onclickDelete()">删除</a>
                <span class="separator"></span>
                <a href="#" class="easyui-linkbutton" iconcls="icon-save" plain="true" onclick="save()">保存</a>
                <a href="#" class="easyui-linkbutton" iconcls="icon-undo" plain="true" onclick="cancel()">放弃</a>
                <span class="separator"></span>
                <a href="#" class="easyui-linkbutton" iconcls="icon-search" plain="true" onclick="onclickSearch()">查询</a>
                <a href="#" class="easyui-linkbutton" iconcls="icon-search" plain="true" onclick="openOrgChart()">组织机构图</a>*@
        </div>
        <div style="width: 30%; float: left;text-align:right">
            <a id="btnCollapse" href="#" class="easyui-linkbutton" iconcls="icon-collapse" plain="true"></a>
        </div>
    </div>
    <div id="pnlSearch" class="datagrid-toolbar-searchField">
        组织名称:<input type="text" id="serhName" placeholder="组织名称" />
    </div>
</div>
<!--/toolbar-->
@section Script{
    <script>

        var editingId;
        var action;//0:添加;1:编辑

        $(function () {
            $('#btnCollapse').click(function () {
                $('#pnlSearch').toggle();
                $('#dg').datagrid('resize');
                var $collapse = $(this).find('.l-btn-text span:first');
                if ($collapse.hasClass('icon-collapse')) {
                    $collapse.removeClass('icon-collapse').addClass('icon-expand');
                } else {
                    $collapse.removeClass('icon-expand').addClass('icon-collapse');
                }
            });
        });

        //点击添加
        function onclickAdd() {
            var t = $('#tg');

            if (editingId != undefined) {
                t.treegrid('select', editingId);
                return;
            }


            var url = '@Url.Action("GetGuid", "Common", new { area=""})';
            $.get(url, function (data) {
                editingId = data;
                var parentId, fullParentId;
                var node = t.treegrid('getSelected');
                if (!node) {
                    parentId = '@Willow.Const.DataBaseConst.DefaultParentId';
                    fullParentId = editingId;
                } else {
                    parentId = node.Id;
                    fullParentId = node.FullParentId + ',' + editingId;
                }

                t.treegrid('append', {
                    parent: parentId,
                    data: [{
                        Id: editingId,
                        ParentId: parentId,
                        IsDelete: false,
                        FullParentId: fullParentId
                    }]
                });
                t.treegrid('beginEdit', editingId);
                action = 0;
            });
        }

        //编辑
        function edit() {
            if (editingId != undefined) {
                $('#tg').treegrid('select', editingId);
                return;
            }
            var row = $('#tg').treegrid('getSelected');
            if (row) {
                editingId = row.Id
                $('#tg').treegrid('beginEdit', editingId);
                action = 1;
            }
        }

        //保存
        function save() {
            if (editingId == undefined) {
                return false;
            }

            var t = $('#tg');
            var validateResult = t.treegrid('validateRow', editingId);

            if (!validateResult) {
                return false;
            }

            t.treegrid('endEdit', editingId);

            var row = t.treegrid('find', editingId);
            row['children'] = null;
            var url = '@Url.Action("Save")';
            var postData = row;
            postData['action'] = action;
            common.post(url, postData, function (result) {
                if (result) {
                    editingId = undefined;
                    action = undefined;
                } else {
                    t.treegrid('beginEdit', editingId);
                }
            });
        }

        //取消
        function cancel() {
            if (editingId != undefined) {
                if (action == 0) {
                    $('#tg').treegrid('remove', editingId);
                } else if (action == 1) {
                    $('#tg').treegrid('cancelEdit', editingId);
                }
                editingId = undefined;
            }
        }

        //点击删除
        function onclickDelete() {
            var t = $('#tg');
            var row = t.treegrid('getSelected');

            if (!row) {
                top.showAlert('请选择信息!', 'warning');
                return false;
            }

            var url = '@Url.Action("Delete")';
            var postData = { id: row.Id };

            common.post(url, postData, function (result) {
                if (result) {
                    t.treegrid('remove', row.Id);
                }
            });
        }

        function onclickSearch() {
            $('#tg').treegrid({
                queryParams: {
                    name: $('#serhName').val()
                }
            });
        }

        //打开组织机构图
        function openOrgChart() {
            top.addTab('组织机构图', '@Url.Action("OrgChart")');
        }

    </script>
}
